<template>
      
  <div class="multiSelect_box" v-show="isMultiSelect">
    <div class="multiSelect_head">
      <div class="multiSelect_close" @click=" $emit('update:isMultiSelect',false)"></div>
    </div>
    <div class="multiSelect_main">
      <div class="choose_handle_item" @click="forwardMore">
        <div class="handle_item_icon more_forwarding"></div>
        <div>转发</div>
      </div>
      <div class="choose_handle_item" @click="toCollection">
        <div class="handle_item_icon option_save"></div>
        <div>收藏</div>
      </div>
      <div class="choose_handle_item">
        <div class="handle_item_icon option_quotations" @click="delChatItem"></div>
        <div>删除</div>
      </div>
    </div>
  </div>
</template>
           
<script>
import { addCollect } from "@/api/home";
import tools from "@/utils/tools";
export default {
  props: [
    "isMultiSelect",
    "chatChooseList",
    "toInfo",
    "sqlTable",
    "myInfo",
    "chatList",
    "sessionList",
    "currentTempChat",
  ],

  methods: {
    // 批量删除聊天多选列表
    delChatItem() {
      if (this.chatChooseList.length <= 0) {
        this.$Message.warning("请选择要删除的内容");
        return;
      }
      let chooseList = "";
      for (let i = 0; i < this.chatChooseList.length; i++) {
        chooseList += `'${this.chatChooseList[i].messageId}',`;
      }
      chooseList = chooseList.replace(/\,$/, "");
      let tableName =
        this.toInfo.isGroup == "1"
          ? this.sqlTable.messageList
          : this.sqlTable.privateList;
      let result = tools.operationalDatabase(
        `DELETE FROM ${tableName} WHERE messageId in (${chooseList});`
      );
      if (result.success == "true") {
        for (let i = 0; i < this.chatChooseList.length; i++) {
          for (let j = 0; j < this.chatList.length; j++) {
            for (let k = 0; k < this.chatList[j].length; k++) {
              if (
                this.chatChooseList[i].messageId ==
                this.chatList[j][k].messageId
              ) {
                this.chatList[j].splice(k, 1);
              }
            }
          }
          for (let j = 0; j < this.currentTempChat.length; j++) {
            //zk1221新增，多选删除同步到currenttempchat
            for (let k = 0; k < this.currentTempChat[j].length; k++) {
              if (
                this.chatChooseList[i].messageId ==
                this.currentTempChat[j][k].messageId
              ) {
                this.currentTempChat[j].splice(k, 1);
              }
            }
          }
        }
        for (let i = 0, len = this.sessionList.length; i < len; i++) {
          if (this.sessionList[i].id == this.toInfo.id) {
            if (this.chatList[0].length > 0) {
              let type = this.chatList[0][0].type;
              let value =
                type == "GPS"
                  ? "[位置]"
                  : type == "file"
                  ? "[文件]"
                  : type == "audio"
                  ? "[语音]"
                  : type == "image"
                  ? "[图片]"
                  : type == "video"
                  ? "[视频]"
                  : type == "shareGroup"
                  ? "[群分享]"
                  : this.chatList[0][0].value;
              this.$set(this.sessionList[i], "value", value || "");
            } else if (this.chatList[1].length > 0) {
              let type = this.chatList[1][0].type;
              let value =
                type == "GPS"
                  ? "[位置]"
                  : type == "file"
                  ? "[文件]"
                  : type == "audio"
                  ? "[语音]"
                  : type == "image"
                  ? "[图片]"
                  : type == "video"
                  ? "[视频]"
                  : type == "shareGroup"
                  ? "[群分享]"
                  : this.chatList[1][0].value;
              this.$set(this.sessionList[i], "value", value || "");
            } else {
              this.$set(this.sessionList[i], "value", "");
            }
          }
        }
      } else {
        this.$Message.warning("删除聊天记录失败");
      }
      this.$emit("update:isMultiSelect", false);
      // this.isMultiSelect = false;
      this.$forceUpdate();
    },

    forwardMore() {
      this.$emit("forwardMore");
    },

    // 多选收藏
    toCollection() {
      if (this.chatChooseList.length <= 0) {
        this.$Message.warning("请选择收藏内容");
        return;
      }
      let chooseList = [];
      for (let i = 0; i < this.chatChooseList.length; i++) {
        // 文本数据类型
        if (this.chatChooseList[i].type == "text") {
          let isTrue = this.toInfo.isGroup == "1";
          let str;
          str = `${
            isTrue ? this.sqlTable.messageList : this.sqlTable.privateList
          } WHERE messageId='${this.chatChooseList[i].messageId}'`;
          let res = tools.operationalDatabase(
            `SELECT * FROM  ${str} ORDER BY time DESC;`,
            1
          );
          if (res.list.length > 0) {
            if (res.list[0].value) {
              this.chatChooseList[i].value = res.list[0].value;
              this.chatChooseList[i].head = res.list[0].value;
            }
          }
          chooseList.push({
            from_name: this.toInfo.name,
            type: this.toInfo.isGroup == "1" ? 1 : 0,
            message_id: this.chatChooseList[i].messageId,
            user_id: this.myInfo.id,
            file_id: this.chatChooseList[i].DownLoadId,
            from_id: this.toInfo.id,
            file_type: this.chatChooseList[i].type,
            value: {
              content: this.chatChooseList[i].value,
              time: this.chatChooseList[i].time,
              receiveName: this.chatChooseList[i].receiveName,
              sendName: this.chatChooseList[i].sendName,
              messageId: this.chatChooseList[i].messageId,
              receiveId: this.chatChooseList[i].receiveId,
              sendId: this.chatChooseList[i].sendId,
              isDownLoad: this.chatChooseList[i].isDownLoad,
              DownLoadId: this.chatChooseList[i].DownLoadId,
              duration: this.chatChooseList[i].duration,
              name: this.chatChooseList[i].name,
              fileName: this.chatChooseList[i].value,
              fileSize: this.chatChooseList[i].duration,
              YJY: "text",
            },
          });
        }
        // 图片数据类型
        if (this.chatChooseList[i].type == "image") {
          chooseList.push({
            from_name: this.toInfo.name,
            type: this.toInfo.isGroup == "1" ? 1 : 0,
            message_id: this.chatChooseList[i].messageId,
            user_id: this.myInfo.id,
            file_id: this.chatChooseList[i].DownLoadId,
            from_id: this.toInfo.id,
            file_type: this.chatChooseList[i].type,
            value: {
              content: this.chatChooseList[i].DownLoadId,
              time: this.chatChooseList[i].time,
              receiveName: this.chatChooseList[i].receiveName,
              sendName: this.chatChooseList[i].sendName,
              messageId: this.chatChooseList[i].messageId,
              receiveId: this.chatChooseList[i].receiveId,
              sendId: this.chatChooseList[i].sendId,
              isDownLoad: this.chatChooseList[i].isDownLoad,
              DownLoadId: this.chatChooseList[i].DownLoadId,
              duration: this.chatChooseList[i].duration,
              name: this.chatChooseList[i].name,
              fileName: this.chatChooseList[i].value,
              fileSize: this.chatChooseList[i].duration,
            },
          });
        }
        // 文件数据类型
        if (this.chatChooseList[i].type == "file") {
          chooseList.push({
            from_name: this.toInfo.name,
            type: this.toInfo.isGroup == "1" ? 1 : 0,
            message_id: this.chatChooseList[i].messageId,
            user_id: this.myInfo.id,
            file_id: this.chatChooseList[i].DownLoadId,
            from_id: this.toInfo.id,
            file_type: this.chatChooseList[i].type,
            value: {
              content: this.chatChooseList[i].DownLoadId,
              time: this.chatChooseList[i].time,
              receiveName: this.chatChooseList[i].receiveName,
              sendName: this.chatChooseList[i].sendName,
              messageId: this.chatChooseList[i].messageId,
              receiveId: this.chatChooseList[i].receiveId,
              sendId: this.chatChooseList[i].sendId,
              isDownLoad: this.chatChooseList[i].isDownLoad,
              DownLoadId: this.chatChooseList[i].DownLoadId,
              duration: this.chatChooseList[i].duration,
              name: this.chatChooseList[i].name,
              fileName: this.chatChooseList[i].value,
              fileSize: this.chatChooseList[i].duration,
            },
          });
        }
        // 2022/6/24 新增文章类型判断
        if (this.chatChooseList[i].type == "article"){
          let msg = JSON.parse(this.chatChooseList[i].value)
          chooseList.push({
            from_name: this.toInfo.name,
            type: this.toInfo.isGroup == "1" ? 1 : 0,
            message_id: this.chatChooseList[i].messageId,
            user_id: this.myInfo.id,
            file_id: this.chatChooseList[i].DownLoadId,
            from_id: this.toInfo.id,
            file_type: this.chatChooseList[i].type,
            value: {
              title: msg.title,
              url: msg.url,
              describe:msg.describe,
              picture:msg.imgUrl||"",
            },
          });
        }
        // 音频数据类型
        if (this.chatChooseList[i].type == "audio") {
          chooseList.push({
            from_name: this.toInfo.name,
            type: this.toInfo.isGroup == "1" ? 1 : 0,
            message_id: this.chatChooseList[i].messageId,
            user_id: this.myInfo.id,
            file_id: this.chatChooseList[i].DownLoadId,
            from_id: this.toInfo.id,
            file_type: this.chatChooseList[i].type,
            value: {
              content: this.chatChooseList[i].value,
              time: this.chatChooseList[i].time,
              receiveName: this.chatChooseList[i].receiveName,
              sendName: this.chatChooseList[i].sendName,
              messageId: this.chatChooseList[i].messageId,
              receiveId: this.chatChooseList[i].receiveId,
              sendId: this.chatChooseList[i].sendId,
              isDownLoad: this.chatChooseList[i].isDownLoad,
              DownLoadId: this.chatChooseList[i].DownLoadId,
              duration: this.chatChooseList[i].duration,
              fontSize: this.chatChooseList[i].fontSize,
              name: this.chatChooseList[i].name,
              fileName: this.chatChooseList[i].value,
              fileSize: this.chatChooseList[i].duration,
            },
          });
        }
        // 视频or地图数据类型 暂不支持收藏
        if (
          this.chatChooseList[i].type == "video" ||
          this.chatChooseList[i].type == "GPS"
        ) {
          this.$Message.success("暂不支持视频和位置收藏");
        }
        addCollect({ collect: JSON.stringify(chooseList) })
          .then((res) => {
            if (res.result == 200) {
              // this.$emit("getCollect");
              if (i === 0) {
                this.$Message.success(res.msg);
                this.$emit("update:isMultiSelect", false);
                // this.isMultiSelect = false;
              }
            } else {
              this.$Message.success(res.msg);
            }
          })
          .catch((error) => {
            this.$Message.success("服务器出错,稍后再试4");
          });
        // this.getCollect();
      }
      // 2022/6/24 修改延迟1秒查收藏，比如添加收藏过多后端返回不及时问题 
      setTimeout(()=>{
        this.$emit("getCollect");
      },1000)
      
    },
  },
};
</script>
<style scoped>
</style>